<template>
  <a-drawer :title="`创建销售提成批次`" width="80%" :visible="visible"
            hide-cancel
            @cancel="visible = false"
            :footer="false"
            v-if="visible">
    <ma-crud ref="crudRef" :columns="columns" :options="options">
      <template #tableButtons>
        <a-button type="primary" status="danger" @click="add()" v-auth="['finance:bonusBill:save']">
          <icon-plus/>
          生成批次
        </a-button>
      </template>
    </ma-crud>
    <batchActionModal :crud-ref="crudRef" :selected="selected" ref="batchActionRef"/>
  </a-drawer>
</template>
<script setup>
import {computed, inject, ref,watch,reactive} from "vue";
import seller from "@/api/system/user.js";
import bonusBill from "@/api/finance/bonusBill.js";
import BatchActionModal from "@cps/actions/batchActionModal.vue";
import {Message} from "@arco-design/web-vue";
const ParentCrudRef = inject('crudRef')
const crudRef = ref()
const batchActionRef = ref()
const selected = computed(() => crudRef.value?.selecteds)
const visible = defineModel()
const businessAt = ref([])
const has_commission = ref('1')
let options = ref({
      rowSelection: {showCheckedAll: true},
      showTools: true,
      expandSearch:true,
      // pageSize:3,
      // autoRequest: false,
      beforeRequest:(params)=>{
        params.has_commission = params.has_commission != undefined ? params.has_commission : 1
        params.business_at = params.business_at ? params.business_at : getCurrentDate()
        businessAt.value = params.business_at
        return params
      },
      requestParams:{
        // role : 'xs',
        scene: 'sellerBill',
      },
      pk: 'id',
      api: seller.getPageList,
    })
const add = () =>{
  batchActionRef.value.open({
        title: '生成批次', api:bonusBill.save, auth: [],
        displayDataIndex: 'username',
        form: {
          form:{
            business_at:businessAt.value
          },
          columns: [
            {
              title: '业务时间', dataIndex: 'business_at',formType: "range",disabled:true,
              rules: {required: true},
            },
            {
              title: '备注', dataIndex: 'remark',formType: "textarea"
            },
          ],
          submited:()=>{
            ParentCrudRef.value.refresh()
          }
        },
      })
}
const getCurrentDate = ()=> {
  const now = new Date();
  const firstDay = new Date(now.getFullYear(), now.getMonth() - 1, 1, 8);
  const lastDay = new Date(now.getFullYear(), now.getMonth(), 0,8);
  return  [firstDay.toISOString().split('T')[0],lastDay.toISOString().split('T')[0]];
}    
const columns = reactive([
  {
      title: "销售",
      dataIndex: "username",
  },
  {
    title: "毛利",
    dataIndex: "gross_profit",
  },
  {
    title: "提成比例",
    dataIndex: "ratio",
  },
  {
    title: "提成",
    dataIndex: "commission",
  },
  {
    title: "销售",
    dataIndex: "seller_id",
    formType: "select",
    virtualListProps:{},
    dict: {
      url: 'system/user/list',
      props: {label: 'username', value: 'id'},
      translation: true,
      params:{select: 'id,username'}
    },
    hide:true,
    search: true,
    multiple:true,
  },
  {
    title: "业务时间",
    dataIndex: "business_at",
    formType: "range",
    search: true,
    hide:true,
    searchDefaultValue:getCurrentDate()
  },
  {
    title: "有业绩",
    dataIndex: "has_commission",
    formType: "select",
    searchDefaultValue:'1',
    dict:{
      data:[{label:'有',value:'1'},{label:'否',value:'2'}],
    },
    search:true,
    hide:true,
  },
])
</script>